<?php

class Api_ProviderController extends Zend_Rest_Controller
{
	
	private $providerTemplate;

   public function init()
    {
        $this->_helper->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    	
    	$this->providerTemplate = array();
    	$this->providerTemplate['ServiceProvider_ID'] = '';
    	$this->providerTemplate['ServiceProvider_name'] ='';
		$this->providerTemplate['ServiceProvider_info'] ='';
		$this->providerTemplate['ServiceProvider_password'] = '';    	
    }
    
    public function getProviderTemplate(){
    	return $this->providerTemplate;
    }
    
    private function authenticate() {
		
    	$requestedID = $this->getRequest()->getParam('id');
    	Zend_Debug::dump($params);
    	//Passwort und Username gesetzt?
    	if (!isset($_SERVER['PHP_AUTH_USER']) OR !isset($_SERVER['PHP_AUTH_PW'])) {
    		
    		header('HTTP/1.0 401 Unauthorized: Authorization required for this section!');
   			exit;
		}
		// PHP_AUTH_USER (=Nutzer) darf nur anfragen zur eigenen ID stellen
		elseif ($requestedID != $_SERVER['PHP_AUTH_USER']){
			header('HTTP/1.0 401 Unauthorized: You may only see your own data!');
   			exit;
		}
		else{
		 	$userID = $_SERVER['PHP_AUTH_USER'];
		 	$password = $_SERVER['PHP_AUTH_PW'];
		 	
		 	$provider = new ServiceProviderModel();
		 	$row = $provider->fetchRow('ServiceProvider_ID=' .(int)$userID);
		 	$dbUserData = $row->toArray();
		 	
		 			 	
		 	if ($dbUserData['ServiceProvider_password'] == $password) {
		 		return;
		 	}
		 	else {
		 		header('HTTP/1.0 401 Unauthorized: Wrong password!');
   				exit;
		 	}
		 } 
    }
    
    

	public function indexAction()
    {
    	Zend_Debug::dump($this->getRequest()->getParam('id'));
         $this->getResponse()
            ->appendBody("From indexAction(), ProviderController returning all providers");
    }

    public function getAction()
    {
    	
    	$this->authenticate();
    	
    	$provider = new ServiceProviderModel();
    	$id = $this->getRequest()->getParam('id');
    	$row = $provider->fetchRow('ServiceProvider_ID=' .$id);
    	$data = $row->toArray();
   		
    	
    	
    	$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    	$xml = $xml . "<ServiceProvider>\n";
    	$xml = $xml . "<ServiceProvider_ID>" . $data['ServiceProvider_ID'] . "</ServiceProvider_ID>\n";
    	$xml = $xml . "<ServiceProvider_name>" . $data['ServiceProvider_name'] . "</ServiceProvider_name>\n";
    	$xml = $xml . "<ServiceProvider_info>" . $data['ServiceProvider_info'] . "</ServiceProvider_info>\n";
    	$xml = $xml . "</ServiceProvider>\n";
    	
    	$response = new SimpleXMLElement($xml);
    	
    	
    	
        $this->getResponse()
        	
        	->setHttpResponseCode(200)
            ->appendBody($response->asXML());
            

    }
    
    public function postAction()
    {
    	//XML DATEN HOLEN
    	$file = $this->getRequest()->getParam('file');
    	if (file_exists($file)){
    		$xml = simplexml_load_file($file);
    	 	
    	}
    	else {
    		
    		$this->getResponse()
				->setHttpResponseCode(404)
				->appendBody("File not Found");
			return;

    	}
    	
		
    	$serviceProvider = new ServiceProviderModel();
    	
    	//Array zur Einspeisung in die DB aufbauen
    	$data = $this->getProviderTemplate();
    	
  		foreach ($xml as $key => $value) {
    	
    			
  				if((string)$key != 'ServiceProvider_ID'){
  					$data[(string)$key] = (string)$value;
  				}
  				else {
  					$data[(string)$key]=null;	
  				}
    				
    				
    		}
    		
    		
    		
    	$id = $serviceProvider->insert($data);
    	
    	
    	
    	//Rückgabe
    	$this->getResponse()
    		->appendBody("Provider with ID " . $id . " was created!\n")
    		->setHttpResponseCode(201);  	
    		
    		
    	

    }
    
    public function putAction()
    {
        $this->getResponse()
            ->appendBody("From putAction() updating the requested Provider");

    }
    
    public function deleteAction()
    {
    	
        $this->getResponse()
            ->appendBody("From deleteAction() of Provider, deleting specified provider");

    }


}

